/*============================================================================
 * Copyright 2009 VMware Inc.
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
 * use this file except in compliance with the License. You may obtain a copy 
 * of the License at: http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations 
 * under the License.
 ============================================================================*/

package com.vmware.vcloud;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

import com.vmware.vcloud.data.VCloudConstants;
import com.vmware.vcloud.rest.RestClient;

/**
 * @author Steve Jin (sjin@vmware.com)
 */

abstract public class VCloudObject 
{
	protected static Element NON_EMPTY_ELEMENT = new DefaultElement("");
	
	public String name;
	public String selfUrl;
	public RestClient rc;
	protected Element root;
	
	public VCloudObject(RestClient rc, String urlPath) throws MalformedURLException
	{
		this(rc, urlPath, null);
	}
	
	public VCloudObject(RestClient rc, String urlPath, String name) throws MalformedURLException
	{
		new URL(urlPath);
		this.selfUrl = urlPath;
		this.rc = rc;
		this.name = name;
	}
	
	public void refresh() throws IOException
	{
		load();
	}
	
	protected void load() throws IOException
	{
		String xml = rc.get(selfUrl);
		System.out.println(xml);
		try
		{
		Document doc = DocumentHelper.parseText(xml);
	    root = doc.getRootElement();
	    this.name = root.attributeValue("name");
		} 
		catch(DocumentException de)
		{
			throw new RuntimeException(de);
		}
	    
	    parse();
	}

	abstract protected void parse() throws MalformedURLException;
	
	public String getDescription()
	{
		return root.elementText(VCloudConstants.ELEM_DESC);
	}

	@SuppressWarnings("unchecked")
	protected void parseVApps(Element vas, List<VApp> vapps) throws MalformedURLException 
	{
		List<Element> elems = vas.elements();
		for(int i=0; i<elems.size(); i++)
		{
			Element elem = elems.get(i);  //vapp tagged element
//			String isTemplate = elem.attributeValue("isTemplate");
//			String name = elem.attributeValue("name");
//			String description = elem.elementText("description");
			
			List<Element> subs = elem.elements();
			for(int j=0; j<subs.size(); j++)
			{
				Element sub = subs.get(j);
				if("self".equals(sub.attributeValue("rel")))
				{
					String href = sub.attributeValue("href");
					VApp va = new VApp(rc, href);
//					va.description = description;
//					va.name = name;
					vapps.add(va);
				}
			}
		}
	}
	
	protected void loadContentIfNotYet() throws IOException
	{
		if(root==null)
		{
				load();
		}
	}
	
	public String getXmlContent() throws IOException
	{
		return rc.get(getURL());
	}
	
	public RestClient getRestClient()
	{
		return rc;
	}
	
	public String getName()
	{
		return this.name;
	}
	
	public String getURL()
	{
		return this.selfUrl;
	}
	
	public RestClient getRC()
	{
		return rc;
	}
	
	public String toString()
	{
		String xmlBody = null;
		
		try
		{
			xmlBody = getXmlContent();
		} catch(IOException ioe)
		{
			ioe.printStackTrace();
		}
		return getURL() + ":" + xmlBody;
	}
}